package zth;

import com.sun.deploy.cache.BaseLocalApplicationProperties;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class LoginServlet  extends HttpServlet {
    private static final long serialVersionUID = 1198764434511986380L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }




    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        String code = request.getParameter("code");
        String passwordyan = null;
        HttpSession session = request.getSession(true);
        String yanzhengcode= (String) session.getAttribute(AuthFilter.LOGIN_VALIDATE_CODE);
        if (yanzhengcode == null || !yanzhengcode.equalsIgnoreCase(code)) {
            response.sendRedirect("/myapp/login.html");
            return;
        }
       // String sql = "update information set number='1' where account='"+user+"'";
        //HttpSession session = request.getSession(true);
        if (user != null && pass != null)
        {
            if (user.equals("admin") && pass.equals("123456")) {
                session.setAttribute(AuthFilter.LOGIN_STATUS, Boolean.TRUE);
                response.sendRedirect("./manager/control.html");
            }
            try {
                passwordyan = DBUtils.GetPassword(user);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            String typeyan = null;
            try {
                typeyan = DBUtils.GetType(user);
            } catch (SQLException e) {
                e.printStackTrace();
            }
//            String numberyan=null;
//            try {
//                numberyan = DBUtils.Getnumber(user);
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
                if (pass.equals(passwordyan) && typeyan.equals("photographer"))
                {
//                    PrintWriter writer = response.getWriter();
//                writer.println("hera!");
//                writer.flush();//ok！
//                    try {
//                        DBUtils.update(sql);
//                    } catch (SQLException e) {
//                        e.printStackTrace();
//                    }
                    response.sendRedirect("./addcontent.html");
                }
//                if(pass.equals(passwordyan) && typeyan.equals("photographer")&&numberyan.equals("1"))
//                {
//                    response.sendRedirect("./photographer/introduce/index.html");
//                }

                if (pass.equals(passwordyan) && typeyan.equals("normal"))
                {
                    response.sendRedirect("./workerlist.jsp");
                }
            }
        else
        {
            response.sendRedirect("./login.html");
        }
        }
    }


